package com.bop.dehong.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;

import com.base.dehong.R;
import com.base.dehong.base.ActivityBaseFrame;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.dialog.NetworkErrorDialog;
import com.bop.dehong.util.LoadingAnimation;
import com.umeng.analytics.MobclickAgent;


public abstract class BaseActivity extends ActivityBaseFrame implements OnClickListener,OnItemClickListener{

	protected String TAG;

	/*
	 * 
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
	 */
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
        MobclickAgent.setDebugMode(false);//开发调试模式下设置开启友盟调试
//      SDK在统计Fragment时，需要关闭Activity自带的页面统计，
//		然后在每个页面中重新集成页面统计的代码(包括调用了 onResume 和 onPause 的Activity)。
		MobclickAgent.openActivityDurationTrack(false);
//		MobclickAgent.setAutoLocation(true);
//		MobclickAgent.setSessionContinueMillis(1000);
//		MobclickAgent.updateOnlineConfig(this);
 		// 添加Activity到堆栈
		ActManager.getAppManager().addActivity(this);
		setContentView(getContentViewId());
		initTop();
		initUI();
		initData();
	}
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
		NetworkErrorDialog.getInstance().dismiss();
		disappearProgressDialog();
		// 结束Activity&从堆栈中移除
		ActManager.getAppManager().finishActivity(this);
	}
	
	LoadingAnimation an ;

	@Override
	public void showProgressDialog() {
		if (an == null) {
			an = new LoadingAnimation(this);
		}
		an.createAnimation();
	}

	@Override
	public void disappearProgressDialog() {
		if (an != null) {
			an.dismissAnimation();
		}
	}

	public static Activity getCurrentActivity(){
		return ActManager.getAppManager().getTopActivity();
	}
 

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart(TAG);
		MobclickAgent.onResume(this);       //统计时长
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd(TAG);
		MobclickAgent.onPause(this);
		ToastUtils.hideToast();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}
	
	/**
	 * 通过类名启动Activity
	 * 
	 * @param pClass
	 */
	protected void openActivity(Class<?> pClass) {
		openActivity(pClass, null);
	}

	/**
	 * 通过类名启动Activity，并且含有Bundle数据
	 * 
	 * @param pClass
	 * @param pBundle
	 */
	protected void openActivity(Class<?> pClass, Bundle pBundle) {
		Intent intent = new Intent(this, pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}

	/**
	 * 通过Action启动Activity
	 * 
	 * @param pAction
	 */
	protected void openActivity(String pAction) {
		openActivity(pAction, null);
	}

	/**
	 * 通过Action启动Activity，并且含有Bundle数据
	 * 
	 * @param pAction
	 * @param pBundle
	 */
	protected void openActivity(String pAction, Bundle pBundle) {
		Intent intent = new Intent(pAction);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}
	/**
	 * 初始化标题,最新处理
	 */
	protected abstract void initTop();
	/**
	 * 初始化UI，，第二处理
	 */
	protected abstract void initUI();
	/**
	 * 初始化数据，，第三处理
	 */
	protected abstract void initData();
	/**
	 * 设置ContentView
	 * @return
	 */
	protected abstract int getContentViewId();
	
	public void onTitleLeftClick(View v){
//		if (!NoDoubleClickUtils.isDoubleClick()){
			finishCurrentActivity();
//		}
		
	}	


	/**
	 * 结束当前Activity
	 * 
	 * @param pClass
	 */
	protected void finishCurrentActivity() {
		ActManager.getAppManager().finishActivity(this);
	}
	/**
	 * 结束所有的应用
	 */
	protected void finishAllActivity() {
		ActManager.getAppManager().finishAllActivity();
	}

    
	public void finishForDelay() {

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				getCurrentActivity().finish();
			}
		}, 1000);
	}
	public void finish()
	{
		super.finish();
		//Activity退出的效果
		overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	}
	

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 结束Activity&从堆栈中移除
			ActManager.getAppManager().finishActivity(this);
			return true;
		 }
		return super.onKeyDown(keyCode, event);
	}
	
	
	public void showNetErrorDialog(){
		
		NetworkErrorDialog.getInstance().show(this);
	}
	
	/**
	 * 注册View的点击事件,然后重写onClick接口函数
	 * @param views
	 */
	public void registerOnClickListener(View...views) {
		for (int i = 0; i < views.length; i++) {
			views[i].setOnClickListener(this);
		}
	}
}
